﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EasyStep.Business.Interfaces;
using EasyStep.Website.Models;
using EasyStep.Data.Entities;
using EasyStep.Business;

namespace EasyStep.Website.Controllers
{
    [Authorize]
    public class TestConstListController : Controller
    {
        private ITestConstantsListManager ConstsList { get; set; }
        private ITestConstantManager Consts { get; set; }
        private ITestProjectManager Projects { get; set; }
        private ITestUserManager Users { get; set; }
        public TestConstListController(ITestConstantManager tConst, ITestConstantsListManager tConstList, ITestProjectManager tProjects, ITestUserManager tUsers)
        {
            this.Consts = tConst;
            this.Projects = tProjects;
            this.Users = tUsers;
            this.ConstsList = tConstList;
        }

        //list of projects & their constant groups
        public ActionResult Index()
        {
            var projectsWithCList = Projects.GetAvailableProjects(Users,ConstsList);
            return View(projectsWithCList);
        }

       

        public ActionResult Create()
        {
            var cProjects= Projects.GetAvailableProjects(Users,ConstsList);
            var cListCount = cProjects.SelectMany(p => p.ConstantLists).Where(cl => cl.Name.StartsWith("new constant list")).Count();
            var model = new TestConstListCreateModel()
            {
                Name = string.Concat("new constant list",cListCount>0?" ["+cListCount.ToString()+"]":""),
                Description="-",
                ProjectNameOrId = cProjects.Count>0? cProjects.FirstOrDefault().Name:""
            };
            
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(TestConstListCreateModel model,FormCollection collection)
        {
            try
            {
                var project = Projects.GetOrCreateProject(model.ProjectNameOrId, Users.Current().Id);
                if (project == null)
                {
                    ModelState.AddModelError("ProjectNameOrId", "Can't find chosen project!");                
                    throw new ArgumentOutOfRangeException("Project hasn't ben generated or found!"+model.ProjectNameOrId);
                }
                
                var newItem = ObjectMapper.ResolveMap<TestConstListCreateModel, TestConstList>(model);
                newItem.ProjectId = project.Id;
                newItem=ConstsList.Create(newItem);
                return RedirectToAction("Details", new { id = newItem.Id });
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("name",ex.Message);                
                return View();                
            }
        }
       

        /// <summary>
        /// here we are using constants editor
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(Guid id)
        {
            var model = ConstsList.GetById(id, Consts, Projects);
            if (!Users.Authorize(model.Project))
                RedirectToAction("Authorize", "Account");
            return View(model);
        }
        /// <summary>
        /// provide a way to edit const list data
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(Guid id)
        {
            var model = ConstsList.GetById(id, Consts, Projects);
            if (!Users.Authorize(model.Project))
                RedirectToAction("Authorize", "Account");
            var viewModel = ObjectMapper.ResolveMap<TestConstList, TestConstListEditModel>(model);
            viewModel.ProjectNameOrId = model.Project.Name;
            return View(viewModel);
        }

        //
        // POST: /TestConstList/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /TestConstList/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /TestConstList/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
